package com.zpark.controller;

import com.zpark.entity.Cart;
import com.zpark.entity.User;
import com.zpark.service.ICartService;
import com.zpark.service.IUserService;
import com.zpark.service.ex.InsertException;
import com.zpark.service.ex.UsernameDuplicateException;
import com.zpark.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zpark.util.JsonResult;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("users")
public class UserController extends BaseController{
    @Autowired
    private IUserService userService;

    @RequestMapping("reg")
    public JsonResult<Void> reg(User user) {
        //创建返回值
        JsonResult<Void> result = new JsonResult<Void>();
//        try {
//            //调用业务对象执行注册
//            userService.reg(user);
//            //响应成功
//            result.setState(200);;
//        } catch (UsernameDuplicateException e){
//            result.setState(4000);
//            result.setMessage("该用户名以及被占用了");
//        } catch (InsertException e){
//            result.setState(5000);
//            result.setMessage("插入异常");
//        }
//        return result;
//    }
        userService.reg(user);
        return new JsonResult<Void>(OK);
    }


    @RequestMapping("login")
    public JsonResult<User> login(String username, String password, HttpSession session){
        User data = userService.login(username, password);
        session.setAttribute("uid", data.getUid());
        session.setAttribute("username", data.getUsername());
        return new JsonResult<User>(OK, data);
    }
}
